package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTDMTietHocService;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocKem;
import com.lb.jwitter.service.TblCTDTMonHocKemService;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblRangBuocHeDaoTaoNhomHocService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinMonHoc;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.service.TblThongTinNhomLopMonHoc;
import com.lb.jwitter.service.TblThongTinNhomLopMonHocService;
import com.lb.jwitter.service.TblThongTinTKBChiTietService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.KetQuaDangKy;
import com.lb.jwitter.util.QuyenDangKy;
import com.lb.jwitter.util.ThoiKhoaBieuDuKien;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class GetThongTinNhomLopMonHocAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	private Long monHocId;

	public Long getMonHocId() {
		return monHocId;
	}

	public void setMonHocId(Long monHocId) {
		this.monHocId = monHocId;
	}

	private List<ThoiKhoaBieuDuKien> thoiKhoaBieuBaiTapDuKiens;

	public List<ThoiKhoaBieuDuKien> getThoiKhoaBieuBaiTapDuKiens() {
		return thoiKhoaBieuBaiTapDuKiens;
	}

	public void setThoiKhoaBieuBaiTapDuKiens(
			List<ThoiKhoaBieuDuKien> thoiKhoaBieuBaiTapDuKiens) {
		this.thoiKhoaBieuBaiTapDuKiens = thoiKhoaBieuBaiTapDuKiens;
	}

	private List<ThoiKhoaBieuDuKien> thoiKhoaBieuDuKiens;


	public List<ThoiKhoaBieuDuKien> getThoiKhoaBieuDuKiens() {
		return thoiKhoaBieuDuKiens;
	}

	public void setThoiKhoaBieuDuKiens(List<ThoiKhoaBieuDuKien> thoiKhoaBieuDuKiens) {
		this.thoiKhoaBieuDuKiens = thoiKhoaBieuDuKiens;
	}


	private int soTietTrongNgay;

	public int getSoTietTrongNgay() {
		return soTietTrongNgay;
	}

	private TblCTDTMonHoc tblCTDTMonHocBaiTap;
	public TblCTDTMonHoc getTblCTDTMonHocBaiTap() {
		return tblCTDTMonHocBaiTap;
	}

	public void setTblCTDTMonHocBaiTap(TblCTDTMonHoc tblCTDTMonHocBaiTap) {
		this.tblCTDTMonHocBaiTap = tblCTDTMonHocBaiTap;
	}

	private TblCTDTMonHoc tblCTDTMonHoc;
	public TblCTDTMonHoc getTblCTDTMonHoc() {
		return tblCTDTMonHoc;
	}

	public void setTblCTDTMonHoc(TblCTDTMonHoc tblCTDTMonHoc) {
		this.tblCTDTMonHoc = tblCTDTMonHoc;
	}

	public void setSoTietTrongNgay(int soTietTrongNgay) {
		this.soTietTrongNgay = soTietTrongNgay;
	}
	
	private TblCTDTDMTietHocService tblCTDTDMTietHocService;
	
	private Boolean trongHanDK;

	public Boolean getTrongHanDK() {
		return trongHanDK;
	}

	public void setTrongHanDK(Boolean trongHanDK) {
		this.trongHanDK = trongHanDK;
	}
	
	private QuyenDangKy quyenDK;

	public QuyenDangKy getQuyenDK() {
		return quyenDK;
	}

	public void setQuyenDK(QuyenDangKy quyenDK) {
		this.quyenDK = quyenDK;
	}
	
	private List<KetQuaDangKy> ketQuaDangKys; 

	public List<KetQuaDangKy> getKetQuaDangKys() {
		return ketQuaDangKys;
	}

	public void setKetQuaDangKys(List<KetQuaDangKy> ketQuaDangKys) {
		this.ketQuaDangKys = ketQuaDangKys;
	}
	
	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{

		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
	     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
	     
	    ApplicationContext context = contextProvider.getApplicationContext(); 
	    
	    TblThongTinNhomLopMonHocService tblThongTinNhomLopMonHocService= (TblThongTinNhomLopMonHocService)  context.getBean("tbl_ThongTin_NhomLop_MonHocClient");
	    TblCTDTMonHocKemService tblCTDTMonHocKemService = (TblCTDTMonHocKemService) context.getBean("tbl_CTDT_MonHocKemClient");

	    TblThongTinTKBChiTietService tblThongTinTKBChiTietService = (TblThongTinTKBChiTietService)  context.getBean("tbl_ThongTin_TKB_ChiTietClient");
		  
	    tblCTDTDMTietHocService = (TblCTDTDMTietHocService) context.getBean("tbl_CTDT_DM_TietHocClient");
	    TblRangBuocHeDaoTaoNhomHocService tblRangBuocHeDaoTaoNhomHocService = 
				(TblRangBuocHeDaoTaoNhomHocService) context.getBean("tbl_RangBuoc_HeDaoTao_NhomHocClient");
		
	    
			List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
			List<String> columns = new ArrayList<String>();
			
			
			
			//get nhomlop mon hoc
			cons.clear();
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("monHocId.id");
		        con.setValue1(monHocId);
		        con.setOperator("eq");
		        cons.add(con);	
		    }
		    if(session.get("dotDKId") != null){ 
		    	int dotDKId = (Integer) session.get("dotDKId");
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("dotDKId.id");
		        con.setValue1(new Long(dotDKId));
		        con.setOperator("eq");
		        cons.add(con);	
		    }
	
		    { 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("ma");
		        con.setValue1("asc");
		        con.setOperator("order");
		        cons.add(con);	
		    }

		    
		    List<TblThongTinNhomLopMonHoc> tblThongTinNhomLopMonHocs = tblThongTinNhomLopMonHocService.findtblThongTinNhomLopMonHocByCondition(cons, 0, 0);
		    if(tblThongTinNhomLopMonHocs != null && tblThongTinNhomLopMonHocs.size() > 0){
		    	
			    tblCTDTMonHoc = tblThongTinNhomLopMonHocs.get(0).getMonHocId();
			    
			    thoiKhoaBieuDuKiens = new ArrayList<ThoiKhoaBieuDuKien>();
			    for(TblThongTinNhomLopMonHoc nhomLop : tblThongTinNhomLopMonHocs){
			    	
			    	ThoiKhoaBieuDuKien thoiKhoaBieuDuKien = new ThoiKhoaBieuDuKien();
			    	thoiKhoaBieuDuKien.setNhomLop(nhomLop);
			    	
			    	cons.clear();
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("TKBId.id");
				        con.setValue1(nhomLop.getTKBId().getId());
				        con.setOperator("eq");
				        cons.add(con);	
				    }
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("ngayHocId.STT");
				        con.setValue1("asc");
				        con.setOperator("order");
				        cons.add(con);	
				    }
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("tietBatDauId.id");
				        con.setValue1("asc");
				        con.setOperator("order");
				        cons.add(con);	
				    }
			    	thoiKhoaBieuDuKien.setChiTiets(tblThongTinTKBChiTietService.findtblThongTinTKBChiTietByCondition(cons, 0, 0));
			    	
			    	thoiKhoaBieuDuKiens.add(thoiKhoaBieuDuKien);
			    }
		    }
		    
		    trongHanDK = (Boolean) session.get("trongHanDK");
		    quyenDK = (QuyenDangKy) session.get("quyenDK");

	    	ketQuaDangKys = (List<KetQuaDangKy>) session.get("ketQuaDangKys");
		    
	    	
	    	/**************************************
	    	 * Mon hoc KEM
	    	 ****************************************/
	    	cons.clear();
	    	{ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("monHocId.id");
		        con.setValue1(monHocId);
		        con.setOperator("eq");
		        cons.add(con);	
		    }
			List<TblCTDTMonHocKem> mhKems = tblCTDTMonHocKemService.findtblCTDTMonHocKemByCondition(cons, 1, 1);
			if(mhKems != null && mhKems.size() > 0){
				System.out.println(mhKems.get(0).getMonHocKemId().getId());
				cons.clear();
			    { 
			        ConditionReport con = new ConditionReport(); 
			        con.setCol("monHocId.id");
			        con.setValue1(mhKems.get(0).getMonHocKemId().getId());
			        con.setOperator("eq");
			        cons.add(con);	
			    }
			    if(session.get("dotDKId") != null){ 
			    	int dotDKId = (Integer) session.get("dotDKId");
			        ConditionReport con = new ConditionReport(); 
			        con.setCol("dotDKId.id");
			        con.setValue1(new Long(dotDKId));
			        con.setOperator("eq");
			        cons.add(con);	
			    }
			    { 
			        ConditionReport con = new ConditionReport(); 
			        con.setCol("ma");
			        con.setValue1("asc");
			        con.setOperator("order");
			        cons.add(con);	
			    }

			    
			    List<TblThongTinNhomLopMonHoc> tblThongTinNhomLopBTs = tblThongTinNhomLopMonHocService.findtblThongTinNhomLopMonHocByCondition(cons, 0, 0);
			    if(tblThongTinNhomLopBTs != null && tblThongTinNhomLopBTs.size() > 0){
			    	
			    	tblCTDTMonHocBaiTap = tblThongTinNhomLopBTs.get(0).getMonHocId();
				    
				    thoiKhoaBieuBaiTapDuKiens= new ArrayList<ThoiKhoaBieuDuKien>();
				    for(TblThongTinNhomLopMonHoc nhomLop : tblThongTinNhomLopBTs){
				    	
				    	ThoiKhoaBieuDuKien thoiKhoaBieuDuKien = new ThoiKhoaBieuDuKien();
				    	thoiKhoaBieuDuKien.setNhomLop(nhomLop);
				    	
				    	cons.clear();
					    { 
					        ConditionReport con = new ConditionReport(); 
					        con.setCol("TKBId.id");
					        con.setValue1(nhomLop.getTKBId().getId());
					        con.setOperator("eq");
					        cons.add(con);	
					    }
					    { 
					        ConditionReport con = new ConditionReport(); 
					        con.setCol("ngayHocId.STT");
					        con.setValue1("asc");
					        con.setOperator("order");
					        cons.add(con);	
					    }
					    { 
					        ConditionReport con = new ConditionReport(); 
					        con.setCol("tietBatDauId.id");
					        con.setValue1("asc");
					        con.setOperator("order");
					        cons.add(con);	
					    }
				    	thoiKhoaBieuDuKien.setChiTiets(tblThongTinTKBChiTietService.findtblThongTinTKBChiTietByCondition(cons, 0, 0));
				    	
				    	thoiKhoaBieuBaiTapDuKiens.add(thoiKhoaBieuDuKien);
				    }
			    }
			    
			}
	    	
	    	
	    	cons.clear();
	    	{ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("ma");
		        con.setValue1("0");
		        con.setOperator("ne");
		        cons.add(con);	
		   }
		    soTietTrongNgay = tblCTDTDMTietHocService.counttblCTDTDMTietHocByCondition(cons);
		    return Action.SUCCESS;

	}


	
	
	
	
	
}
